package com.ssii.www.myproject.web.controller;

import com.ssii.www.myproject.common.util.AjaxResult;
import com.ssii.www.myproject.common.util.LogUtils;
import com.ssii.www.myproject.common.util.PageList;
import com.ssii.www.myproject.domain.System;
import com.ssii.www.myproject.query.SystemQuery;
import com.ssii.www.myproject.service.SystemService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/system")
public class SystemController {
  @Autowired
  private SystemService systemService;

  private static final Logger logger = LogUtils.getLogger();

  @RequestMapping("/index")
  @RequiresPermissions("system:index")
  public String index() {
    return "system/index";
  }

  @RequestMapping("/page")
  @ResponseBody
  public Map<String,Object> page(SystemQuery query) {
    Map<String,Object> map = new HashMap<>();
    if (StringUtils.isNotBlank(query.getQ())) {
      query.setName(query.getQ());
    }
    Page<System> pageByQuery = systemService.findPageByQuery(query);
    map.put("rows",pageByQuery.getContent());
    map.put("page",pageByQuery.getTotalPages());
    map.put("total",pageByQuery.getTotalElements());
    return map;
  }

  /**
   * 根据id删除
   *
   * @param id
   * @return
   */
  @RequestMapping("/delete")
  @ResponseBody
  public AjaxResult delete(Long id) {
    systemService.delete(id);
    return new AjaxResult();
  }

  /**
   * 添加
   *
   * @return
   */
  @ResponseBody
  @RequestMapping("/save")
  public AjaxResult save(System system) {
    systemService.save(system);
    return new AjaxResult();
  }


}
